package com.hunttown.common.sharding.client.transaction;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.CollectionUtils;

public class DefaultMultipleTransactionService implements MultipleTransactionService, ApplicationContextAware, InitializingBean {
    private ApplicationContext springContext;
    private Map<String, Object> transactionManagerConfig;
    private Map<String, PlatformTransactionManager> transactionManagerMap;

    public DefaultMultipleTransactionService() {
    }

    public void setAllTransactionManager(Map<String, Object> allTransactionManager) {
        this.transactionManagerConfig = allTransactionManager;
    }

    public void afterPropertiesSet() throws Exception {
        if (CollectionUtils.isEmpty(this.transactionManagerConfig)) {
            throw new IllegalArgumentException("allTransactionManager cann't be null or empty!");
        } else {
            this.transactionManagerMap = new HashMap(this.transactionManagerConfig.size());
            Iterator i$ = this.transactionManagerConfig.entrySet().iterator();

            while (i$.hasNext()) {
                Entry<String, Object> entity = (Entry) i$.next();
                Object value = entity.getValue();
                if (value instanceof PlatformTransactionManager) {
                    this.transactionManagerMap.put(entity.getKey(), (PlatformTransactionManager) value);
                } else {
                    if (!(value instanceof String)) {
                        throw new IllegalArgumentException("illegal value argument,key:" + (String) entity.getKey() + ",a reference or bean name of type PlatformTransactionManager is required!");
                    }

                    Object valueBean = this.springContext.getBean((String) value);
                    if (!(valueBean instanceof PlatformTransactionManager)) {
                        throw new IllegalArgumentException("illegal value argument,key:" + (String) entity.getKey() + ",a reference or bean name of type PlatformTransactionManager is required!");
                    }

                    this.transactionManagerMap.put(entity.getKey(), (PlatformTransactionManager) valueBean);
                }
            }

        }
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.springContext = applicationContext;
    }

    public Map<String, PlatformTransactionManager> getTransactionManagers() {
        if (CollectionUtils.isEmpty(this.transactionManagerMap)) {
            this.transactionManagerMap = Collections.emptyMap();
        }

        return Collections.unmodifiableMap(this.transactionManagerMap);
    }
}